layui.define(['element', 'api', 'header','utils','econsole', 'content', 'footer' ,'drag'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'app',
		$ = layui.jquery,
        header = layui.header,
        footer = layui.footer,
        content = layui.content,
        utils = layui.utils,
		api = layui.api,
        econsole = layui.econsole
    ;

	var app = function(opt) {
		this.option = opt;
	};


    function APP(option){
        var ajaxHeaderParams = option.ajaxHeaderParams;
        var id = option.id;
        //增加ajax拦截，在请求中头中增加tenantId参数
        ah.proxy({
            //请求发起前进入
            onRequest: function(config, handler){
                if(id){
                    config.headers.tenantId = id;
                }

                //设置自定义head参数
                for (var item in ajaxHeaderParams){
                    if(ajaxHeaderParams[item]){
                        config.headers[item] = ajaxHeaderParams[item];
                    }
                }

                handler.next(config);
            },
            //请求发生错误时进入，比如超时；注意，不包括http状态码错误，如404仍然会认为请求成功
            onError: function(err, handler){
                handler.next(err)
            },
            //请求成功后进入
            onResponse: function(response, handler){
                handler.next(response)
            }
        })
    }



    /**
	 * Button start loading
	 * */
    app.prototype.render = function(opt) {

		var option = {
		    elem: opt.elem,
            id: opt.id,
            title: opt.title,
            logo: opt.logo,
			env:opt.env,
            version: opt.version, 
            enableFooter: opt.enableFooter || true,
            headerSettings:opt.headerSettings,
            footerSettings:opt.footerSettings || {version: opt.version},
            ajaxHeaderParams:opt.ajaxHeaderParams,
            contentSettings: opt.contentSettings
		}

        APP(option);

        //渲染头部区域
        if(option.headerSettings){
            option.headerSettings.elem = option.elem;
            header.render(option.headerSettings);
        }

        //渲染内容区
        if(option.contentSettings){
            option.contentSettings.elem = option.elem;
            //渲染内容区
            content.render(option.contentSettings);
        }

        //渲染内容区
        if(option.enableFooter){
            option.footerSettings.elem = option.elem;
            //渲染底部区域
            footer.render(option.footerSettings);
        }



        return new app(option);
	}


    exports(MOD_NAME, new app());
});
